//
//  EndGameLayer.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 5/16/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "EndGameLayer.h"
#import "Global.h"
#import "GameLayer.h"

@implementation EndGameLayer
@synthesize newScore;
@synthesize heightScore;
+(CCScene*)scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	EndGameLayer *layer = [EndGameLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(id)init
{
    if (self = [super init]) {
        CGSize s = [[CCDirector sharedDirector] winSize];
        CCSprite *bg = [CCSprite spriteWithFile:@"bg_endgame.png"];
        [self addChild:bg z:-100];
        bg.anchorPoint = ccp(0.5, 0);
        bg.position = ccp(s.width*0.5, 0);
        
        CCSprite *timeUP = [CCSprite spriteWithFile:@"end_timeup.png"];
        timeUP.anchorPoint = ccp(0.5, 1);
        timeUP.position = ccp(s.width*0.5, s.height - 10);
        [self addChild:timeUP];
        
        CCSprite *stars = [[[CCSprite alloc] init] autorelease];
        [self addChild:stars];
        stars.position = ccp(s.width *0.5, s.height *0.5);
        
        NSMutableArray *starArr = [[NSMutableArray alloc] init];
        for (int i = 1; i <=11 ; i++) {
            CCSpriteFrame *frame = [CCSpriteFrame frameWithTextureFilename:[NSString stringWithFormat:@"ani_star_%d.png", i] rect:CGRectMake(0, 0, 222, 200)];
            [starArr addObject:frame];
        }
        CCAnimation *a = [[CCAnimation alloc]initWithSpriteFrames:starArr delay:0.2];
        [stars runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:a]]];
        
        CCSprite*lbNewScores = [CCSprite spriteWithFile:@"end_newscore.png"];
        lbNewScores.anchorPoint = ccp(1, 0.5);
        lbNewScores.position = ccp(s.width*0.5, s.height*0.7);
        [self addChild:lbNewScores];
        
        lbNewScores = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%d", [Global getLastestScore]] fntFile:@"end_number.fnt"];
        lbNewScores.anchorPoint = ccp(-0.01, 0.5);
        lbNewScores.position = ccp(s.width*0.5, s.height*0.7);
        [self addChild:lbNewScores];
        
        CCSprite *lbHeightScores = [CCSprite spriteWithFile:@"end_hightscore.png"];
        lbHeightScores.anchorPoint = ccp(1, 0.5f);
        lbHeightScores.position = ccp(s.width*0.5, s.height * 0.5);
        [self addChild:lbHeightScores];
        
        lbHeightScores = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%d", [Global getHeightestScore]] fntFile:@"end_number.fnt"];
        lbHeightScores.anchorPoint = ccp(-0.01, 0.5f);
        lbHeightScores.position = ccp(s.width*0.5, s.height*0.5);
        [self addChild:lbHeightScores];
        
        CCMenuItem *gameCenter = [CCMenuItemImage itemWithNormalImage:@"gamecenter.png" selectedImage:@"gamecenter.png" block:^(id sender) {
            
        }];
        
        CCMenuItem *facebook = [CCMenuItemImage itemWithNormalImage:@"facebook.png" selectedImage:@"facebook.png" block:^(id sender) {
            
        }];
        
        CCMenuItem *twiter = [CCMenuItemImage itemWithNormalImage:@"twitter_11.png" selectedImage:@"twitter_11.png" block:^(id sender) {
            
        }];
        
        CCMenuItem *home = [CCMenuItemImage itemWithNormalImage:@"btn_home_normal.png" selectedImage:@"btn_home_normal.png" block:^(id sender) {
            [appDelegate showStartScreen];
        }];
        
        CCMenuItem *retry = [CCMenuItemImage itemWithNormalImage:@"btn_retry_normal.png" selectedImage:@"btn_retry_normal.png" block:^(id sender) {
            [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[GameLayer scene]]];
        }];
        
        CCMenu *menu = [CCMenu menuWithItems:home, retry, gameCenter, facebook, twiter, nil];
        menu.position = CGPointZero;
        menu.anchorPoint = CGPointZero;
        [self addChild:menu];
        home.anchorPoint = ccp(1.3, 0.5);
        home.position = ccp(s.width *0.5, s.height*0.1);
        retry.anchorPoint = ccp(-0.3, 0.5);
        retry.position = ccp(s.width*0.5, s.height*0.1);
        gameCenter.position = ccp(s.width *0.5 - 55, s.height*0.3);
        facebook.position = ccp(s.width *0.5, s.height*0.3);
        twiter.position = ccp(s.width *0.5 + 55, s.height*0.3);
    }
    return self;
}

-(void)setHeightScore:(int)heightScore_
{
    heightScore = heightScore_;
    lbHeightScore.string = [NSString stringWithFormat:@"%d", heightScore];
}

-(void)setNewScore:(int)newScore_
{
    newScore = newScore_;
    lbNewScore.string = [NSString stringWithFormat:@"%d", newScore];
}
@end
